package mi.payment.utils.transaction;

import android.content.Context;
import android.text.TextUtils;

import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 退货
 */
public class ReturnGoodsTask extends TransactionTask<Object, Void, TradeData> {

    public static final String TYPE_RETURN_GOODS = "0220";
    public static final String TYPE_BATCH_UPLOAD = "0320";

    protected ReturnGoodsTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(Object[] objects) {
        tradeData = (TradeData) params[0];
        String msgType = (String) params[1];
        dbBean = tradeData.getPayDetail();
        if (msgType.equals(TYPE_RETURN_GOODS)) {
            setDBStorage(true);
        }
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_SALES_RETURNS);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        String msgType = (String) params[1];
        // 交易处理码(域3)
        dbBean.setProcessCode("200000");
        // 交易类型(域60)
        dbBean.setTransType("25");
        // 网络管理信息码(域60)
        dbBean.setNetManageCode("000");
        // 本地交易时间戳
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);
        // 交易应答码(域39)
        dbBean.setTradeAnswerCode("96");

        // 更新预组装的Map
        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        // 盛付通退貨60域需要上送全
        String value60 = dbBean.getTransType() + dbBean.getBathNo() + "000601";
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 填写61域
        MsgField fieldF061 = map.get("F061");
        String value61;
        if (TextUtils.isEmpty(dbBean.getOriginalPOSNum())) {
            value61 = "000000000000" + dbBean.getOriginalTransDate();
        } else {
            value61 = dbBean.getBathNo() + dbBean.getOriginalPOSNum() + dbBean.getOriginalTransDate();
        }
        fieldF061.setDataStr(value61);
        fieldF061.setDataLength(value61.length());

        // 填写63域
        MsgField fieldF063 = map.get("F063");
        fieldF063.setDataStr("000");
        fieldF063.setDataLength(3);

        // 批上送通知交易,退货交易,预授权完成(通知)离线结算,结算调整逐笔上送报文
        // 使用原报文格式,将信息类型码改为"0320/0330"
        // 去掉MAC域64,订购退货与订购预授权交易不上送。
        if (msgType.equals(TYPE_BATCH_UPLOAD)) {
            map.remove("F064");
        }

        // 添加自定义的商户号
        map = ParseUtils.addCustomTransID(map);

        // 组包，得到组包后的报文
        return EncodePackage.assembly(map, msgType, TerminalSecure.PROCESSREQUEST_NONE);
    }


}
